import { useUserStoreHook } from "@/store/modules/user";
import { router } from "@/router";
import { message } from "@/utils/message";
import { closeAllDialog } from "@/components/ReDialog";

export const baseUrlApi = (url: string) =>
  process.env.NODE_ENV === "development"
    ? `http://127.0.0.1:9532/admin/${url}`
    : `https://group.zhoong.cn/interface/admin/${url}`;

// 添加响应处理函数
export const handleApiResponse = response => {
  // 检查 log_out 字段
  if (response.log_out === true) {
    message("登录已过期，请重新登录", { type: "error" });
    // 先关闭所有弹窗
    closeAllDialog();
    useUserStoreHook().logOut();
    router.push("/login");
    return new Promise(() => {});
  }
  return response;
};

// 添加错误处理函数
export const handleApiError = error => {
  // 检查错误响应中的数据
  if (error.response?.log_out === true) {
    message("登录已过期，请重新登录", { type: "error" });
    // 先关闭所有弹窗
    closeAllDialog();
    useUserStoreHook().logOut();
    router.push("/login");
    return new Promise(() => {});
  }
  return Promise.reject(error);
};
